<?php
namespace app\admin\controller;
use app\admin\model\GoodscatsModel;
use think\Controller;
use think\Request;
use app\admin\controller\Base;
class Goodscats extends Base{
    public function index(){
        $this->isLogin();
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->goodsCatsList();
        $this->assign('rows',$rows);
        return $this->fetch('index');
    }
    //删除商品分类
    public function delGoodsCats(){
        $id=input('id',0);
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->delGoodsCats($id);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }


    }
    //新增一级分类
    public function addFirstCats(){
        $this->isLogin();
        return $this->fetch('add_first_cats');
    }
    //新增一级分类
    public function doAddFirstCats(){
        $data['type_name']=input('type_name');
        $data['parent_id']=0;
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->doAddFirstCats($data);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }
    }
    //新增子分类
    public function addChildCart(){
        $this->isLogin();
        $id=input('id',0);
        $this->assign('id',$id);
        return $this->fetch('add_child_cart');
    }
    //新增子分类
    public function doAddChildCart(){
        $data['type_name']=input('type_name');
        $data['parent_id']=input('id',0);
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->doAddChildCart($data);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }
    }
    //修改分类
    public function updateCat(){
        $this->isLogin();
        $data['id']=input('id',0);
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->updateCat($data);
        $this->assign('rows',$rows);
        return $this->fetch('update_cat');
    }
    //修改分类
    public function doUpdateCat(){
        $data['type_name']=input('type_name');
        $chickid['id']=input('id',0);
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->doUpdateCat($data,$chickid);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }
    }
    //商品详情图片表
    public function goodsInfoImagesList(){
        $this->isLogin();
        $Goodscats=new GoodscatsModel();
        $rows=$Goodscats->goodsInfoImagesList();
        $page = $rows->render();
        $this->assign('rows',$rows);
        $this->assign('page',$page);
        return $this->fetch('goods_info_images_list');
    }
    //添加商品详情图
    public function addGoodsInfo(){
        //三级联动获取分类
        $firstcat=new  GoodscatsModel();
        $rows=$firstcat->getFirstCat();
        $this->assign('rows',$rows);
        return $this->fetch('add_goods_info');
    }
    //三级联动 ajax获取
    public function getFirstCatId(){
        $catId=input('catId',0);
        $firstcat=new  GoodscatsModel();
        $rows=$firstcat->getFirstCatId($catId);
        print_r(json_encode($rows));
    }
    //三级联动 ajax获取二级分类下的商品
    public function getSecondCatId(){
        $catId=input('catId',0);
        $firstcat=new  GoodscatsModel();
        $rows=$firstcat->getSecondCatId($catId);
        print_r(json_encode($rows));
    }
    //图片上传
    public function uploadPic(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload/shop');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $getSaveName = $info->getSaveName();
                print_r($getSaveName);
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

    }
    //图片上传
    public function uploadAllGoodsImages(){
        $goodsurl=input('goodsurl',0);
        $getThreeCatId=input('getThreeCatId',0);
        $goodsdescribe=input('goodsdescribe');
        $arrgoodsurl = explode(',',$goodsurl);
        $data['goods_id']=$getThreeCatId;
        $data['goods_describe']=$goodsdescribe;
        if( is_array($arrgoodsurl)  ){
            foreach( $arrgoodsurl  as  $v  ){
                $arr[]=$v;
            }
            if(  count($arr)==1  ){
                $data['goods_info_images1']=$arr[0];
            }elseif( count($arr)==2 ){
                $data['goods_info_images1']=$arr[0];
                $data['goods_info_images2']=$arr[1];
            }elseif( count($arr)==3 ){
                $data['goods_info_images1']=$arr[0];
                $data['goods_info_images2']=$arr[1];
                $data['goods_info_images3']=$arr[2];
            }elseif( count($arr)==4  ){
                $data['goods_info_images1']=$arr[0];
                $data['goods_info_images2']=$arr[1];
                $data['goods_info_images3']=$arr[2];
                $data['goods_info_images4']=$arr[3];
            }
        }
        $uploadAll=new  GoodscatsModel();
        $rows=$uploadAll->uploadAllGoodsImages($data);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }
    }
    //删除商品详情
    public function deleteGoodsInfo(){
        $id=input('id',0);
        $deleteGoodsInfo=new  GoodscatsModel();
        $rows=$deleteGoodsInfo->deleteGoodsInfo($id);
        $this->redirect('Goodscats/goodsInfoImagesList');
    }
    //商品评价
    public function goodsAppraises(){
        $this->isLogin();
        $goodsAppraises=new  GoodscatsModel();
        $rows=$goodsAppraises->goodsAppraises();
        $page = $rows->render();
        $this->assign('rows',$rows);
        $this->assign('page',$page);
        return $this->fetch('goods_appraises');
    }
    //删除商品评价
    public function deleteGoodsAppraises(){
        $id=input('id',0);
        $deleteGoodsAppraises=new  GoodscatsModel();
        $rows=$deleteGoodsAppraises->deleteGoodsAppraises($id);
        if($rows >0){
            echo 1;
        }else{
            echo  -1;
        }
    }



}